package com.example.learn_gl_android.anglib.util.math

import android.opengl.Matrix
import com.example.learn_gl_android.anglib.util.math.Matrix4x4.Companion
import java.util.Vector

data class Matrix3x3(val elements: FloatArray = FloatArray(3*3)) : Cloneable {

    companion object {
        fun one() : Matrix3x3 {
            val ret = Matrix3x3()
            ret.elements[0] = 1f
            ret.elements[4] = 1f
            ret.elements[8] = 1f
            return ret
        }
    }


    operator fun plus(other: Matrix3x3) : Matrix3x3{
        val ret = FloatArray(3*3)
        for ( i in 0..8 ){
            ret[i] = elements[i] + other.elements[i]
        }
        return Matrix3x3(ret)
    }

    operator fun plusAssign(other: Matrix4x4){
        for ( i in 0..8 ){
            elements[i] = elements[i] + other.elements[i]
        }
    }

    operator fun minus(other: Matrix3x3) : Matrix3x3{
        val ret = FloatArray(3*3)
        for ( i in 0..8 ){
            ret[i] = elements[i] - other.elements[i]
        }
        return Matrix3x3(ret)
    }

    operator fun minusAssign(other: Matrix3x3){
        for ( i in 0..8 ){
            elements[i] = elements[i] - other.elements[i]
        }
    }

    operator fun times (other : Matrix3x3) : Matrix3x3 {
        val ret = one()
        Matrix.multiplyMM(ret.elements, 0,
            this.elements, 0,
            other.elements,  0)
        return ret
    }

    operator fun timesAssign (other : Matrix3x3) {
        val copy = this.elements.clone()

        Matrix.multiplyMM(this.elements, 0,
            copy, 0,
            other.elements,  0)
    }


    override fun clone() : Matrix3x3{

        return  Matrix3x3(this.elements.clone())
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Matrix3x3

        return elements.contentEquals(other.elements)
    }

    override fun hashCode(): Int {
        return elements.contentHashCode()
    }

}